﻿using BLL;
using Model;
using System;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class SequencesSettingEdit : BaseUI.BasePage
    {
        private Qs_SequencesBLL sequencesBL = new Qs_SequencesBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var table = Request.QueryString["TABLE"];
                var col = Request.QueryString["COL"];
                if (string.IsNullOrEmpty(table))
                {
                    FORM_IFEDIT.Value = "FALSE";
                    FORM_TITLE_LABLE.InnerText = "新\n增\n序\n列";
                    FORM_START_NUMBER.Text = "0";
                    FORM_CURRENT_NUMBER.Text = "0";
                    FORM_LAST_NUMBER.Text = "1";
                    FORM_IDENTITY_VALUE.Text = "1";
                }
                else
                {
                    FORM_IFEDIT.Value = "TRUE";
                    FORM_TITLE_LABLE.InnerText = "修\n改\n序\n列";

                    Qs_Sequences entity = sequencesBL.Find(table, col);
                    if (entity == null)
                    {
                        MessageBox.Show(this, "【温馨提示】数据不存在");
                        return;
                    }
                    FORM_TABLE_NAME.Text = entity.CurC_Table_Name;
                    FORM_TABLE_NAME.Enabled = true;
                    FORM_COL_NAME.Text = entity.CurC_Col_Name;
                    FORM_COL_NAME.Enabled = true;
                    FORM_START_NUMBER.Text = entity.CurN_Start_Number.ToString().Trim();
                    FORM_CURRENT_NUMBER.Text = entity.CurN_Current_Number.ToString().Trim();
                    FORM_LAST_NUMBER.Text = entity.CurN_Next_Number.ToString().Trim();
                    FORM_IDENTITY_VALUE.Text = entity.N_Identity_Value.ToString().Trim();
                }
            }
        }

        protected void SAVE_CLICK(object sender, EventArgs e)
        {
            if (FORM_IFEDIT.Value == "FALSE")
            {
                #region 新增

                try
                {
                    var entity = new Qs_Sequences
                    {
                        CurC_Table_Name = FORM_TABLE_NAME.Text.Trim().ToUpper(),
                        CurC_Col_Name = FORM_COL_NAME.Text.Trim().ToUpper(),
                        CurN_Current_Number = Convert.ToDecimal(FORM_CURRENT_NUMBER.Text.Trim()),
                        CurN_Next_Number = Convert.ToDecimal(FORM_LAST_NUMBER.Text.Trim()),
                        CurN_Start_Number = Convert.ToDecimal(FORM_START_NUMBER.Text.Trim()),
                        N_Identity_Value = Convert.ToDecimal(FORM_IDENTITY_VALUE.Text.Trim())
                    };
                    if (sequencesBL.Find(entity.CurC_Table_Name, entity.CurC_Col_Name) != null)
                    {
                        MessageBox.Show(this, "【温馨提示】 序列已存在");
                        return;
                    }
                    if (sequencesBL.INSERT(entity) > 0)
                    {
                        #region 载入数据

                        FORM_IFEDIT.Value = "TRUE";
                        FORM_TITLE_LABLE.InnerText = "修\n改\n序\n列";

                        FORM_TABLE_NAME.Text = entity.CurC_Table_Name;
                        FORM_TABLE_NAME.Enabled = true;
                        FORM_COL_NAME.Text = entity.CurC_Col_Name;
                        FORM_COL_NAME.Enabled = true;
                        FORM_START_NUMBER.Text = entity.CurN_Start_Number.ToString().Trim();
                        FORM_CURRENT_NUMBER.Text = entity.CurN_Current_Number.ToString().Trim();
                        FORM_LAST_NUMBER.Text = entity.CurN_Next_Number.ToString().Trim();
                        FORM_IDENTITY_VALUE.Text = entity.N_Identity_Value.ToString().Trim();

                        #endregion 载入数据

                        MessageBox.Show(this, "【温馨提示】 新增成功");
                        return;
                    }
                    MessageBox.Show(this, "【温馨提示】 新增失败");
                    return;
                }
                catch (Exception)
                {
                    MessageBox.Show(this, "【温馨提示】 异常");
                    return;
                }

                #endregion 新增
            }
            else
            {
                #region 修改

                var entity = sequencesBL.Find(FORM_TABLE_NAME.Text, FORM_COL_NAME.Text);
                if (entity == null)
                {
                    MessageBox.Show(this, "【温馨提示】 没有找到数据");
                    return;
                }
                entity.CurN_Current_Number = Convert.ToDecimal(FORM_CURRENT_NUMBER.Text.Trim());
                entity.CurN_Next_Number = Convert.ToDecimal(FORM_LAST_NUMBER.Text.Trim());
                entity.CurN_Start_Number = Convert.ToDecimal(FORM_START_NUMBER.Text.Trim());
                entity.N_Identity_Value = Convert.ToDecimal(FORM_IDENTITY_VALUE.Text.Trim());
                if (sequencesBL.UPDATE(entity) > 0)
                {
                    MessageBox.Show(this, "【温馨提示】 修改成功");
                    return;
                }
                MessageBox.Show(this, "【温馨提示】 修改失败");
                return;

                #endregion 修改
            }
        }
    }
}